package ie.dcu.spca.data.memory;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import ie.dcu.spca.data.BasicDataManager;
import ie.dcu.spca.model.Bean;

public abstract class MemoryBasicDataManager implements BasicDataManager {

    private static final int ID_DEFAULT_VALUE = 0;
    private static int id = ID_DEFAULT_VALUE;

    protected static void resetId() {
        id = ID_DEFAULT_VALUE;
    }

    protected static int getId() {
        return ++id;
    }

    protected static long now() {
        return System.currentTimeMillis();
    }

    protected int getPositionByBeanId(List<Bean> list, int id) {
        int i = 0;
        for (Bean b : list) {
            if (getId(b) == id) {
                break;
            }
            i++;
        }
        return i;
    }

    protected int getPositionByBeanId(int id) {
        return getPositionByBeanId(getBeanList(), id);
    }

    @Override
    public List<Map<String, Object>> getIdNameMapList() {
        return getIdNameMapList(getBeanList());
    }

    @Override
    public List<Map<String, Object>> getIdNameMapList(List<Bean> beanList) {
        List<Map<String, Object>> beanML = new LinkedList<Map<String, Object>>();
        for (Bean b : beanList) {
            Map<String, Object> beanM = new HashMap<String, Object>();
            beanM.put(ID, getId(b));
            beanM.put(NAME, getName(b));
            beanML.add(beanM);
        }
        return beanML;
    }

}